<!DOCTYPE html>

































































<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="shortcut icon" type="image/x-icon" href="../../../favicon.ico" />
<title>Camera | Android Developers</title>
<link href="../../../assets/android-developer-docs-devguide.css" rel="stylesheet" type="text/css" />
<script src="../../../assets/search_autocomplete.js" type="text/javascript"></script>
<script src="../../../assets/jquery-resizable.min.js" type="text/javascript"></script>
<script src="../../../assets/android-developer-docs.js" type="text/javascript"></script>
<script src="../../../assets/prettify.js" type="text/javascript"></script>
<script type="text/javascript">
  setToRoot("../../../");
</script>
<noscript>
  <style type="text/css">
    html,body{overflow:auto;}
    #body-content{position:relative; top:0;}
    #doc-content{overflow:visible;border-left:3px solid #666;}
    #side-nav{padding:0;}
    #side-nav .toggle-list ul {display:block;}
    #resize-packages-nav{border-bottom:3px solid #666;}
  </style>
</noscript>
</head>
<body class="gc-documentation">

  <div id="header">
      <div id="headerLeft">
          <a href="../../../index.html" tabindex="-1"><img
              src="../../../assets/images/bg_logo.png" alt="Android Developers" /></a>
          <ul id="header-tabs" class="guide">
    
	<li id="home-link"><a href="../../../offline.html">
	
		<span class="en">Home</span>
		<span style="display:none" class="de">Startseite</span>
		<span style="display:none" class="es"></span>
		<span style="display:none" class="fr"></span>
		<span style="display:none" class="it"></span>
		<span style="display:none" class="ja">ホーム</span>
		<span style="display:none" class="zh-CN">主页</span>
		<span style="display:none" class="zh-TW">首頁</span>
	
	</a></li>
	<li id="sdk-link"><a href="../../../sdk/index.html">
		<span class="en">SDK</span>
	</a></li>
	<li id="guide-link"><a href="../../../guide/index.html" onClick="return loadLast('guide')">
	
		<span class="en">Dev Guide</span>
		<span style="display:none" class="de">Handbuch</span>
		<span style="display:none" class="es">Guía</span>
		<span style="display:none" class="fr">Guide</span>
		<span style="display:none" class="it">Guida</span>
		<span style="display:none" class="ja">開発ガイド</span>
		<span style="display:none" class="zh-CN">开发人员指南</span>
		<span style="display:none" class="zh-TW">開發指南</span>
	
	</a></li>
	<li id="reference-link"><a href="../../../reference/packages.html" onClick="return loadLast('reference')">
	
		<span class="en">Reference</span>
		<span style="display:none" class="de">Referenz</span>
		<span style="display:none" class="es">Referencia</span>
		<span style="display:none" class="fr">Référence</span>
		<span style="display:none" class="it">Riferimento</span>
		<span style="display:none" class="ja">リファレンス</span>
		<span style="display:none" class="zh-CN">参考</span>
		<span style="display:none" class="zh-TW">參考資料</span>
	
	</a></li>
	<li id="resources-link"><a href="../../../resources/index.html" onClick="return loadLast('resources')">
	
		<span class="en">Resources</span>
		<span style="display:none" class="de"></span>
		<span style="display:none" class="es"></span>
		<span style="display:none" class="fr"></span>
		<span style="display:none" class="it"></span>
    		<span style="display:none" class="ja"></span>
		<span style="display:none" class="zh-CN"></span>
		<span style="display:none" class="zh-TW"></span>
	
	</a></li>
	<li id="videos-link"><a href="../../../videos/index.html" onClick="return loadLast('videos')">
	
		<span class="en">Videos</span>
		<span style="display:none" class="de"></span>
		<span style="display:none" class="es"></span>
		<span style="display:none" class="fr"></span>
		<span style="display:none" class="it"></span>
		<span style="display:none" class="ja">ビデオ</span>
		<span style="display:none" class="zh-CN"></span>
		<span style="display:none" class="zh-TW"></span>
	
	</a></li>
	<li><a href="http://android-developers.blogspot.com" onClick="return requestAppendHL(this.href)">
	
		<span class="en">Blog</span>
		<span style="display:none" class="de"></span>
		<span style="display:none" class="es"></span>
		<span style="display:none" class="fr"></span>
		<span style="display:none" class="it"></span>
		<span style="display:none" class="ja">ブログ</span>
		<span style="display:none" class="zh-CN">博客</span>
		<span style="display:none" class="zh-TW">網誌</span>
	
	</a></li>


     
</ul>
     
      </div>
      <div id="headerRight">
          <div id="headerLinks">
          
          <a href="http://www.android.com">Android.com</a>
          </div>
  <div id="search" >
      <div id="searchForm">
          <form accept-charset="utf-8" class="gsc-search-box" 
                onsubmit="return submit_search()">
            <table class="gsc-search-box" cellpadding="0" cellspacing="0"><tbody>
                <tr>
                  <td class="gsc-input">
                    <input id="search_autocomplete" class="gsc-input" type="text" size="33" autocomplete="off"
                      title="search developer docs" name="q"
                      value="search developer docs"
                      onFocus="search_focus_changed(this, true)"
                      onBlur="search_focus_changed(this, false)"
                      onkeydown="return search_changed(event, true, '../../../')"
                      onkeyup="return search_changed(event, false, '../../../')" />
                  <div id="search_filtered_div" class="no-display">
                      <table id="search_filtered" cellspacing=0>
                      </table>
                  </div>
                  </td>
                  <td class="gsc-search-button">
                    <input type="submit" value="Search" title="search" id="search-button" class="gsc-search-button" />
                  </td>
                  <td class="gsc-clear-button">
                    <div title="clear results" class="gsc-clear-button">&nbsp;</div>
                  </td>
                </tr></tbody>
              </table>
          </form>
      </div><!-- searchForm -->
  </div><!-- search -->
      </div><!-- headerRight -->
      <script type="text/javascript">
        <!--  
        changeTabLang(getLangPref());
        //-->
      </script>
  </div><!-- header -->

  <div class="g-section g-tpl-240" id="body-content">
    <div class="g-unit g-first" id="side-nav">
      <div id="devdoc-nav">
<ul>

  <li>
    <h2><span class="en">Android Basics</span>
        <span class="de" style="display:none">Einführung in Android</span>
        <span class="es" style="display:none">Información básica sobre Android</span>
        <span class="fr" style="display:none">Présentation d'Android</span>
        <span class="it" style="display:none">Nozioni di base su Android</span>
        <span class="ja" style="display:none">Android の基本</span>
        <span class="zh-CN" style="display:none">Android 基础知识</span>
        <span class="zh-TW" style="display:none">Android 簡介</span>
    </h2>
    <ul>
      <li><a href="../../../guide/basics/what-is-android.html">
        <span class="en">What Is Android?</span>
        <span class="de" style="display:none">Was ist Android?</span>
        <span class="es" style="display:none">¿Qué es Android?</span>
        <span class="fr" style="display:none">Qu'est-ce qu'Android&nbsp;?</span>
        <span class="it" style="display:none">Che cos'è Android?</span>
        <span class="ja" style="display:none">Android とは</span>
        <span class="zh-CN" style="display:none">Android 是什么？</span>
        <span class="zh-TW" style="display:none">什麼是 Android？</span>
          </a></li>
      <li><a href="../../../guide/topics/fundamentals.html">
        <span class="en">Application Fundamentals</span>
        <span class="de" style="display:none">Anwendungsgrundlagen</span>
        <span class="es" style="display:none">Fundamentos de las aplicaciones</span>
        <span class="fr" style="display:none">Principes de base des applications</span>
        <span class="it" style="display:none">Concetti fondamentali sulle applicazioni</span>
        <span class="ja" style="display:none">開発の基礎</span>
        <span class="zh-CN" style="display:none">应用程序基础</span>
        <span class="zh-TW" style="display:none">應用程式基本原理</span>
      </a></li>

  <!--  <li><a style="color:gray;">The Android SDK</a></li> -->
  <!--  <li><a style="color:gray;">Walkthrough for Developers</a></li> -->
      <!-- quick overview of what it's like to develop on Android -->
    </ul>
  </li>

  <li>
    <h2>
      <span class="en">Framework Topics</span>
      <span class="de" style="display:none">Framework-Themen</span>
      <span class="es" style="display:none">Temas sobre el framework</span>
      <span class="fr" style="display:none">Thèmes relatifs au framework</span>
      <span class="it" style="display:none">Argomenti relativi al framework</span>
      <span class="ja" style="display:none">フレームワーク トピック</span>
      <span class="zh-CN" style="display:none">框架主题</span>
      <span class="zh-TW" style="display:none">架構主題</span>
    </h2>
    <ul>
      <li class="toggle-list">
        <div><a href="../../../guide/topics/fundamentals/activities.html">
          <span class="en">Activities</span>
        </a></div>
        <ul>
          <li><a href="../../../guide/topics/fundamentals/fragments.html">
            <span class="en">Fragments</span>
          </a></li>
          <li><a href="../../../guide/topics/fundamentals/loaders.html">
            <span class="en">Loaders</span>
          </a></li>
          <li><a href="../../../guide/topics/fundamentals/tasks-and-back-stack.html">
            <span class="en">Tasks and Back Stack</span></a></li>
        </ul>
      </li>
      <li class="toggle-list">
        <div><a href="../../../guide/topics/fundamentals/services.html">
          <span class="en">Services</span>
        </a></div>
        <ul>
          <li><a href="../../../guide/topics/fundamentals/bound-services.html">
            <span class="en">Bound Services</span>
          </a></li>
        </ul>
      </li>
      <li><a href="../../../guide/topics/providers/content-providers.html">
            <span class="en">Content Providers</span>
          </a></li>
      <li><a href="../../../guide/topics/intents/intents-filters.html">
            <span class="en">Intents and Intent Filters</span>
          </a></li>
      <li><a href="../../../guide/topics/fundamentals/processes-and-threads.html">
            <span class="en">Processes and Threads</span>
          </a></li>
    </ul>


    <ul>
      <li class="toggle-list">
        <div><a href="../../../guide/topics/ui/index.html">
            <span class="en">User Interface</span>
          </a></div>
        <ul>
          <li><a href="../../../guide/topics/ui/declaring-layout.html">
               <span class="en">XML Layouts</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/ui-events.html">
                <span class="en">Input Events</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/menus.html">
               <span class="en">Menus</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/actionbar.html">
               <span class="en">Action Bar</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/dialogs.html">
                <span class="en">Dialogs</span>
              </a></li>
          <li class="toggle-list">
            <div><a href="../../../guide/topics/ui/notifiers/index.html">
                <span class="en">Notifications</span>
            </a></div>
            <ul>
              <li><a href="../../../guide/topics/ui/notifiers/toasts.html">
                <span class="en">Toast Notifications</span>
              </a></li>
              <li><a href="../../../guide/topics/ui/notifiers/notifications.html">
                <span class="en">Status Bar Notifications</span>
              </a></li>
            </ul>
          </li>
          <li><a href="../../../guide/topics/ui/drag-drop.html">
                <span class="en">Drag and Drop</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/themes.html">
                <span class="en">Styles and Themes</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/custom-components.html">
                <span class="en">Custom Components</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/binding.html">
                <span class="en">Binding to Data with AdapterView</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/layout-objects.html">
                <span class="en">Common Layout Objects</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/how-android-draws.html">
                <span class="en">How Android Draws Views</span>
              </a></li>
        </ul>
      </li><!-- end of User Interface -->

      <li class="toggle-list">
        <div><a href="../../../guide/topics/resources/index.html">
               <span class="en">Application Resources</span>
             </a></div>
        <ul>
          <li><a href="../../../guide/topics/resources/providing-resources.html">
                <span class="en">Providing Resources</span>
              </a></li>
          <li><a href="../../../guide/topics/resources/accessing-resources.html">
                <span class="en">Accessing Resources</span>
              </a></li>
          <li><a href="../../../guide/topics/resources/runtime-changes.html">
                <span class="en">Handling Runtime Changes</span>
              </a></li>
          <li><a href="../../../guide/topics/resources/localization.html">
                <span class="en">Localization</span>
              </a></li>
          <li class="toggle-list">
            <div><a href="../../../guide/topics/resources/available-resources.html">
              <span class="en">Resource Types</span>
            </a></div>
            <ul>
              <li><a href="../../../guide/topics/resources/animation-resource.html">Animation</a></li>
              <li><a href="../../../guide/topics/resources/color-list-resource.html">Color State List</a></li>
              <li><a href="../../../guide/topics/resources/drawable-resource.html">Drawable</a></li>
              <li><a href="../../../guide/topics/resources/layout-resource.html">Layout</a></li>
              <li><a href="../../../guide/topics/resources/menu-resource.html">Menu</a></li>
              <li><a href="../../../guide/topics/resources/string-resource.html">String</a></li>
              <li><a href="../../../guide/topics/resources/style-resource.html">Style</a></li>
              <li><a href="../../../guide/topics/resources/more-resources.html">More Types</a></li>
            </ul>
          </li><!-- end of resource types -->
        </ul>
      </li><!-- end of app resources -->
      <li class="toggle-list">
        <div><a href="../../../guide/topics/data/data-storage.html">
            <span class="en">Data Storage</span>
          </a></div>
          <ul>
            <li><a href="../../../guide/topics/data/backup.html">
                <span class="en">Data Backup</span>
              </a>
            </li>
          </ul>
      </li>
      <li><a href="../../../guide/topics/security/security.html">
            <span class="en">Security and Permissions</span>
          </a></li>
      <li class="toggle-list">
        <div><a href="../../../guide/topics/manifest/manifest-intro.html">
          <span class="en">The AndroidManifest.xml File</span>
        </a></div>
        <ul>
          <li><a href="../../../guide/topics/manifest/action-element.html">&lt;action&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/activity-element.html">&lt;activity&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/activity-alias-element.html">&lt;activity-alias&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/application-element.html">&lt;application&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/category-element.html">&lt;category&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/compatible-screens-element.html">&lt;compatible-screens&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/data-element.html">&lt;data&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/grant-uri-permission-element.html">&lt;grant-uri-permission&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/instrumentation-element.html">&lt;instrumentation&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/intent-filter-element.html">&lt;intent-filter&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/manifest-element.html">&lt;manifest&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/meta-data-element.html">&lt;meta-data&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/path-permission-element.html">&lt;path-permission&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/permission-element.html">&lt;permission&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/permission-group-element.html">&lt;permission-group&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/permission-tree-element.html">&lt;permission-tree&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/provider-element.html">&lt;provider&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/receiver-element.html">&lt;receiver&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/service-element.html">&lt;service&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/supports-gl-texture-element.html">&lt;supports-gl-texture&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/supports-screens-element.html">&lt;supports-screens&gt;</a></li><!-- ##api level 4## -->
          <li><a href="../../../guide/topics/manifest/uses-configuration-element.html">&lt;uses-configuration&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/uses-feature-element.html">&lt;uses-feature&gt;</a></li> <!-- ##api level 4## -->
          <li><a href="../../../guide/topics/manifest/uses-library-element.html">&lt;uses-library&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/uses-permission-element.html">&lt;uses-permission&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/uses-sdk-element.html">&lt;uses-sdk&gt;</a></li>
        </ul>
      </li><!-- end of the manifest file -->
    </ul>

    <ul>
    <li class="toggle-list">
        <div><a href="../../../guide/topics/graphics/index.html">
            <span class="en">Graphics</span>
          </a><span class="new-child">new!</span></div>
        <ul>
          <li><a href="../../../guide/topics/graphics/2d-graphics.html">
              <span class="en">Canvas and Drawables</span></a></li>
          <li><a href="../../../guide/topics/graphics/hardware-accel.html">
              <span class="en">Hardware Acceleration</span></a>
            <span class="new">new!</span></li>
          <li><a href="../../../guide/topics/graphics/opengl.html">
              <span class="en">OpenGL</span>
            </a><span class="new">updated</span></li>
        </ul>
      </li>
      <li class="toggle-list">
        <div><a href="../../../guide/topics/graphics/animation.html">
            <span class="en">Animation</span>
          </a></div>
        <ul>
          <li><a href="../../../guide/topics/graphics/prop-animation.html">
              <span class="en">Property Animation</span></a></li>
          <li><a href="../../../guide/topics/graphics/view-animation.html">
              <span class="en">View Animation</span></a></li>
          <li><a href="../../../guide/topics/graphics/drawable-animation.html">
              <span class="en">Drawable Animation</span></a></li>
        </ul>
      </li>
      <li class="toggle-list">
	        <div><a href="../../../guide/topics/renderscript/index.html">
	            <span class="en">RenderScript</span>
	          </a></div>
	        <ul>
	          <li><a href="../../../guide/topics/renderscript/graphics.html">
	                <span class="en">Graphics</span>
	              </a>
	          </li>
	          <li><a href="../../../guide/topics/renderscript/compute.html">
	                <span class="en">Compute</span>
	              </a>
	          </li>
	        </ul>
  	  </li>

      <li class="toggle-list">
          <div><a href="../../../guide/topics/media/index.html">
            <span class="en">Multimedia and Camera</span>
          </a><span class="new">updated</span></div>
          <ul>
            <li><a href="../../../guide/topics/media/mediaplayer.html">
                  <span class="en">Media Playback</span></a>
                </li>
            <li><a href="../../../guide/topics/media/jetplayer.html">
                  <span class="en">JetPlayer</span></a>
                </li>
            <li><a href="../../../guide/topics/media/camera.html">
                  <span class="en">Camera</span></a>
                  <span class="new">new!</span>
                </li>
            <li><a href="../../../guide/topics/media/audio-capture.html">
                  <span class="en">Audio Capture</span></a>
                </li>
          </ul>
      </li>
      <li>
        <a href="../../../guide/topics/clipboard/copy-paste.html">
            <span class="en">Copy and Paste</span>
        </a></li>
  <!--<li class="toggle-list">
        <div><a style="color:gray;">Sensors</a></div>
          <ul>
            <li><a style="color:gray;">Camera</a></li>
            <li><a style="color:gray;">Compass</a></li>
            <li><a style="color:gray;">Accelerometer</a></li>
          </ul>
      </li> -->
      <li class="toggle-list">
        <div><a href="../../../guide/topics/location/index.html">
               <span class="en">Location and Maps</span>
             </a></div>
        <ul>
          <li><a href="../../../guide/topics/location/obtaining-user-location.html">
                <span class="en">Obtaining User Location</span>
              </a></li>
        </ul>
      </li>
  <!--<li class="toggle-list">
        <div><a style="color:gray;">Wireless Controls</a></div>
          <ul>
            <li><a style="color:gray;">Wi-Fi</a></li>
          </ul>
      </li> -->
  <!--<li><a style="color:gray;">Localization</a></li>  -->
      <li><a href="../../../guide/topics/appwidgets/index.html">
            <span class="en">App Widgets</span></a>
          </li>
      <li><a href="../../../guide/topics/wireless/bluetooth.html">
            <span class="en">Bluetooth</span></a>
          </li>
      <li><a href="../../../guide/topics/nfc/index.html">
            <span class="en">Near Field Communication</span>
          </a></li>
      <li class="toggle-list">
          <div><a href="../../../guide/topics/usb/index.html">
            <span class="en">USB</span></a>
          </div>
            <ul>
              <li><a href="../../../guide/topics/usb/accessory.html">Accessory</a></li>
              <li><a href="../../../guide/topics/usb/host.html">Host</a></li>
              <li><a href="../../../guide/topics/usb/adk.html">Open Accessory Dev Kit</a></li>
            </ul>
          </li>

       <li><a href="../../../guide/topics/network/sip.html">
            <span class="en">Session Initiation Protocol</span>
          </a></li>
      <li class="toggle-list">
        <div><a href="../../../guide/topics/search/index.html">
            <span class="en">Search</span>
          </a></div>
          <ul>
            <li><a href="../../../guide/topics/search/search-dialog.html">Creating a Search Interface</a></li>
            <li><a href="../../../guide/topics/search/adding-recent-query-suggestions.html">Adding Recent Query Suggestions</a></li>
            <li><a href="../../../guide/topics/search/adding-custom-suggestions.html">Adding Custom Suggestions</a></li>
            <li><a href="../../../guide/topics/search/searchable-config.html">Searchable Configuration</a></li>
          </ul>
      </li>
      <li><a href="../../../guide/topics/admin/device-admin.html">
            <span class="en">Device Administration</span></a>
      </li>
      <li class="toggle-list">
           <div>
                <a href="../../../guide/topics/testing/index.html">
                   <span class="en">Testing</span>
               </a>
           </div>
           <ul>
              <li>
                <a href="../../../guide/topics/testing/testing_android.html">
                <span class="en">Testing Fundamentals</span></a>
              </li>
              <li>
                <a href="../../../guide/topics/testing/activity_testing.html">
                <span class="en">Activity Testing</span></a>
              </li>
              <li>
                <a href="../../../guide/topics/testing/contentprovider_testing.html">
                <span class="en">Content Provider Testing</span></a>
              </li>
              <li>
                <a href="../../../guide/topics/testing/service_testing.html">
                <span class="en">Service Testing</span></a>
              </li>
              <li>
                <a href="../../../guide/topics/testing/what_to_test.html">
                <span class="en">What To Test</span></a>
              </li>
           </ul>
      </li>
    </ul>
  </li>

  <li>
    <h2>
      <span class="en">Android Market Topics</span>
    </h2>
    <ul>
      <li><a href="../../../guide/publishing/licensing.html">
          <span class="en">Application Licensing</span></a>
      </li>
      <li class="toggle-list">
        <div><a href="../../../guide/market/billing/index.html">
            <span class="en">In-app Billing</span></a>
        </div>
        <ul>
          <li><a href="../../../guide/market/billing/billing_overview.html">
              <span class="en">In-app Billing Overview</span></a>
          </li>
          <li><a href="../../../guide/market/billing/billing_integrate.html">
              <span class="en">Implementing In-app Billing</span></a>
          </li>
          <li><a href="../../../guide/market/billing/billing_best_practices.html">
              <span class="en">Security and Design</span></a>
          </li>
          <li><a href="../../../guide/market/billing/billing_testing.html">
              <span class="en">Testing In-app Billing</span></a>
          </li>
          <li><a href="../../../guide/market/billing/billing_admin.html">
              <span class="en">Administering In-app Billing</span></a>
          </li>
          <li><a href="../../../guide/market/billing/billing_reference.html">
              <span class="en">In-app Billing Reference</span></a>
          </li>
        </ul>
      </li>
      <li><a href="../../../guide/appendix/market-filters.html">
          <span class="en">Market Filters</span></a>
      </li>
      <li><a href="../../../guide/market/publishing/multiple-apks.html">
          <span class="en">Multiple APK Support</span></a>
      </li>
    </ul>
  </li>


  <li>
    <h2><span class="en">Developing</span>
               <span class="de" style="display:none">Entwicklung</span>
               <span class="es" style="display:none">Desarrollo</span>
               <span class="fr" style="display:none">Développement</span>
               <span class="it" style="display:none">Sviluppo</span>
               <span class="ja" style="display:none">開発</span>
               <span class="zh-CN" style="display:none">开发</span>
               <span class="zh-TW" style="display:none">開發</span>
    </h2>
    <ul>
  <!--<li><a href="">Developing for Android</a></li>
      signing, upgrading, selecting a package name, select device profile, touch, trackball, dpad available, etc. -->
      <li>
        <a href="../../../guide/developing/index.html">
        <span class="en">Introduction</span></a>
      </li>

      <li class="toggle-list">
        <div>
           <a href="../../../guide/developing/devices/index.html">
                <span class="en">Managing Virtual Devices</span>
            </a>
        </div>
        <ul>
          <li>
            <a href="../../../guide/developing/devices/managing-avds.html">
              <span class="en">With AVD Manager</span>
            </a>
          </li>
          <li>
            <a href="../../../guide/developing/devices/managing-avds-cmdline.html">
              <span class="en">From the Command Line</span>
            </a>
          </li>
          <li>
           <a href="../../../guide/developing/devices/emulator.html">
                <span class="en">Using the Android Emulator</span>
            </a>
          </li>
        </ul>
      </li>
      <li>
        <a href="../../../guide/developing/device.html">
          <span class="en">Using Hardware Devices</span>
        </a>
      </li>

      <li class="toggle-list">
        <div>
          <a href="../../../guide/developing/projects/index.html">
            <span class="en">Managing Projects</span>
          </a>
        </div>
        <ul>
          <li>
            <a href="../../../guide/developing/projects/projects-eclipse.html">
              <span class="en">From Eclipse with ADT</span>
            </a>
          </li>
          <li>
            <a href="../../../guide/developing/projects/projects-cmdline.html">
                <span class="en">From the Command Line</span>
            </a>
          </li>
        </ul>
      </li>

      <li class="toggle-list">
        <div>
          <a href="../../../guide/developing/building/index.html">
            <span class="en">Building and Running</span>
          </a>
        </div>
        <ul>
          <li><a href="../../../guide/developing/building/building-eclipse.html">
            <span class="en">From Eclipse with ADT</span>
          </a></li>
          <li><a href="../../../guide/developing/building/building-cmdline.html">
            <span class="en">From the Command Line</span></a></li>
        </ul>
      </li>

      <li class="toggle-list">
        <div>
          <a href="../../../guide/developing/debugging/index.html">
            <span class="en">Debugging</span>
          </a>
        </div>
        <ul>
          <li>
            <a href="../../../guide/developing/debugging/debugging-projects.html">
                <span class="en">From Eclipse with ADT</span>
            </a>
          </li>
          <li>
            <a href="../../../guide/developing/debugging/debugging-projects-cmdline.html">
                <span class="en">From Other IDEs</span>
            </a>
          </li>
          <li>
            <a href="../../../guide/developing/debugging/ddms.html">
              <span class="en">Using DDMS</span>
            </a>
          </li>
          <li>
            <a href="../../../guide/developing/debugging/debugging-log.html">
                <span class="en">Reading and Writing Logs</span>
            </a>
          </li>
          <li>
            <a href="../../../guide/developing/debugging/debugging-ui.html">
                <span class="en">Debugging and Profiling UIs</span>
            </a>
          </li>
          <li>
            <a href="../../../guide/developing/debugging/debugging-tracing.html">
                <span class="en">Profiling with Traceview and dmtracedump</span>
            </a>
          </li>
          <li>
            <a href="../../../guide/developing/debugging/debugging-devtools.html">
                <span class="en">Using the Dev Tools App</span>
            </a>
          </li>
        </ul>
      </li>

      <li class="toggle-list">
           <div>
                <a href="../../../guide/developing/testing/index.html">
                   <span class="en">Testing</span>
               </a>
           </div>
           <ul>
              <li>
                <a href="../../../guide/developing/testing/testing_eclipse.html">
                  <span class="en">From Eclipse with ADT</span>
                </a>
              </li>

              <li>
                <a href="../../../guide/developing/testing/testing_otheride.html">
                  <span class="en">From Other IDEs</span>
                </a>
              </li>
           </ul>
         </li>

         <li class="toggle-list">
        <div><a href="../../../guide/developing/tools/index.html">
            <span class="en">Tools</span>
          </a></div>
        <ul>
          <li><a href="../../../guide/developing/tools/adb.html">adb</a></li>
          <li><a href="../../../guide/developing/tools/adt.html">ADT</a></li>
          <li><a href="../../../guide/developing/tools/android.html">android</a></li>
          <li><a href="../../../guide/developing/tools/bmgr.html">bmgr</a>
          <li><a href="../../../guide/developing/tools/dmtracedump.html">dmtracedump</a></li>
          <li><a href="../../../guide/developing/tools/draw9patch.html">Draw
          9-Patch</a></li>
          <li><a href="../../../guide/developing/tools/emulator.html">Emulator</a></li>
          <li><a href="../../../guide/developing/tools/etc1tool.html">etc1tool</a></li>
          <li><a href="../../../guide/developing/tools/hierarchy-viewer.html">Hierarchy Viewer</a></li>
          <li><a href="../../../guide/developing/tools/hprof-conv.html">hprof-conv</a></li>
          <li><a href="../../../guide/developing/tools/layoutopt.html">layoutopt</a></li>
          <li><a href="../../../guide/developing/tools/logcat.html">logcat</a></li>
          <li><a href="../../../guide/developing/tools/mksdcard.html">mksdcard</a></li>
          <li><a href="../../../guide/developing/tools/monkey.html">Monkey</a></li>
          <li class="toggle-list">
            <div><a href="../../../guide/developing/tools/monkeyrunner_concepts.html">
              <span class="en">monkeyrunner</span>
            </a></div>
            <ul>
              <li><a href="../../../guide/developing/tools/MonkeyDevice.html">
                <span class="en">MonkeyDevice</span>
                </a></li>
              <li><a href="../../../guide/developing/tools/MonkeyImage.html">
                <span class="en">MonkeyImage</span>
                </a></li>
              <li><a href="../../../guide/developing/tools/MonkeyRunner.html">
                <span class="en">MonkeyRunner</span>
                </a></li>
            </ul>
          </li>
          <li><a href="../../../guide/developing/tools/proguard.html">ProGuard</a></li>
          <li><a href="../../../guide/developing/tools/adb.html#sqlite">sqlite3</a></li>
          <li><a href="../../../guide/developing/tools/traceview.html">Traceview</a></li>
          <li><a href="../../../guide/developing/tools/zipalign.html">zipalign</a></li>
        </ul>
      </li>
    </ul>
  </li>

  <li>
    <h2><span class="en">Publishing</span>
        <span class="de" style="display:none">Veröffentlichung</span>
        <span class="es" style="display:none">Publicación</span>
        <span class="fr" style="display:none">Publication</span>
        <span class="it" style="display:none">Pubblicazione</span>
        <span class="ja" style="display:none">公開</span>
        <span class="zh-CN" style="display:none">发布</span>
        <span class="zh-TW" style="display:none">發佈</span>
    </h2>
    <ul>
      <li><a href="../../../guide/publishing/app-signing.html">
            <span class="en">Signing Your Applications</span>
            <span class="de" style="display:none">Signieren Ihrer Anwendungen</span>
            <span class="es" style="display:none">Firma de aplicaciones</span>
            <span class="fr" style="display:none">Attribution de votre signature <br />à vos applications</span>
            <span class="it" style="display:none">Firma delle applicazioni</span>
            <span class="ja" style="display:none">アプリケーションへの署名</span>
            <span class="zh-CN" style="display:none">应用程序签名</span>
            <span class="zh-TW" style="display:none">簽署應用程式</span>
          </a></li>
      <li><a href="../../../guide/publishing/versioning.html">
            <span class="en">Versioning Your Applications</span>
            <span class="de" style="display:none">Versionsverwaltung für Ihre <br />Anwendungen</span>
            <span class="es" style="display:none">Versiones de las aplicaciones</span>
            <span class="fr" style="display:none">Attribution d'une version à vos applications</span>
            <span class="it" style="display:none">Controllo versioni delle applicazioni</span>
            <span class="ja" style="display:none">アプリケーションのバージョニング</span>
            <span class="zh-CN" style="display:none">应用程序版本控制</span>
            <span class="zh-TW" style="display:none">應用程式版本設定</span>
          </a></li>
      <li><a href="../../../guide/publishing/preparing.html">
            <span class="en">Preparing to Publish</span>
            <span class="de" style="display:none">Vorbereitung auf die Veröffentlichung</span>
            <span class="es" style="display:none">Publicación de aplicaciones</span>
            <span class="fr" style="display:none">Préparation à la publication</span>
            <span class="it" style="display:none">Preparativi per la pubblicazione</span>
            <span class="ja" style="display:none">公開の準備</span>
            <span class="zh-CN" style="display:none">准备发布</span>
            <span class="zh-TW" style="display:none">準備發佈</span>
          </a></li>
      <li><a href="../../../guide/publishing/publishing.html">
            <span class="en">Publishing on Android Market</span>
          </a></li>
    </ul>
  </li>

  <li>
    <h2><span class="en">Best Practices</span>
               <span class="de" style="display:none">Bewährte Verfahren</span>
               <span class="es" style="display:none">Prácticas recomendadas</span>
               <span class="fr" style="display:none">Meilleures pratiques</span>
               <span class="it" style="display:none">Best practice</span>
               <span class="ja" style="display:none">ベスト プラクティス</span>
               <span class="zh-CN" style="display:none">最佳实践</span>
               <span class="zh-TW" style="display:none">最佳實務</span>
    </h2>
    <ul>
      <li><a href="../../../guide/practices/compatibility.html">
            <span class="en">Compatibility</span>
          </a></li>
      <li class="toggle-list">
        <div><a href="../../../guide/practices/screens_support.html">
          <span class="en">Supporting Multiple Screens</span>
        </a></div>
        <ul>
          <li><a href="../../../guide/practices/screens-distribution.html">
            <span class="en">Distributing to Specific Screens</span>
          </a></li>
          <li><a href="../../../guide/practices/screen-compat-mode.html">
            <span class="en">Screen Compatibility Mode</span>
          </a></li>
          <li><a href="../../../guide/practices/screens-support-1.5.html">
            <span class="en">Strategies for Android 1.5</span>
          </a></li>
        </ul>
      </li>
      <li><a href="../../../guide/practices/optimizing-for-3.0.html">
            <span class="en">Optimizing Apps for Android 3.0</span>
          </a></li>
      <li class="toggle-list">
        <div><a href="../../../guide/practices/ui_guidelines/index.html">
               <span class="en">UI Guidelines</span>
             </a></div>
        <ul>
          <li class="toggle-list">
            <div><a href="../../../guide/practices/ui_guidelines/icon_design.html">
                   <span class="en">Icon Design <span class="new">updated</span></span>
                 </a></div>
            <ul>
              <li><a href="../../../guide/practices/ui_guidelines/icon_design_launcher.html">
                    <span class="en">Launcher Icons <span class="new">updated</span></span>
                  </a></li>
              <li><a href="../../../guide/practices/ui_guidelines/icon_design_menu.html">
                    <span class="en">Menu Icons</span>
                  </a></li>
              <li><a href="../../../guide/practices/ui_guidelines/icon_design_action_bar.html">
                    <span class="en">Action Bar Icons <span class="new">new!</span></span>
                  </a></li>
              <li><a href="../../../guide/practices/ui_guidelines/icon_design_status_bar.html">
                    <span class="en">Status Bar Icons <span class="new">updated</span></span>
                  </a></li>
              <li><a href="../../../guide/practices/ui_guidelines/icon_design_tab.html">
                    <span class="en">Tab Icons</span>
                  </a></li>
              <li><a href="../../../guide/practices/ui_guidelines/icon_design_dialog.html">
                    <span class="en">Dialog Icons</span>
                  </a></li>
              <li><a href="../../../guide/practices/ui_guidelines/icon_design_list.html">
                    <span class="en">List View Icons</span>
                  </a></li>
            </ul>
          </li>
          <li><a href="../../../guide/practices/ui_guidelines/widget_design.html">
                <span class="en">App Widget Design <span class="new">updated</span></span>
              </a></li>
          <li><a href="../../../guide/practices/ui_guidelines/activity_task_design.html">
                <span class="en">Activity and Task Design</span>
              </a></li>
          <li><a href="../../../guide/practices/ui_guidelines/menu_design.html">
                <span class="en">Menu Design</span>
              </a></li>
        </ul>
      </li>
      </ul>
      <ul>
      <li><a href="../../../guide/practices/design/accessibility.html">
            <span class="en">Designing for Accessibility</span>
          </a></li>
      <li class="toggle-list">
        <div><a href="../../../guide/practices/design/performance.html">
            <span class="en">Designing for Performance</span>
          </a></div>
        <ul>
          <li><a href="../../../guide/practices/design/jni.html">
                <span class="en">JNI Tips</span>
              </a></li>
        </ul>
      </li>
      <li><a href="../../../guide/practices/design/responsiveness.html">
            <span class="en">Designing for Responsiveness</span>
          </a></li>
      <li><a href="../../../guide/practices/design/seamlessness.html">
            <span class="en">Designing for Seamlessness</span>
          </a></li>
    </ul>
  </li>

  <li>
    <h2><span class="en">Web Applications</span>
    </h2>
    <ul>
      <li><a href="../../../guide/webapps/index.html">
            <span class="en">Web Apps Overview</span>
          </a></li>
      <li><a href="../../../guide/webapps/targeting.html">
            <span class="en">Targeting Screens from Web Apps</span>
          </a></li>
      <li><a href="../../../guide/webapps/webview.html">
            <span class="en">Building Web Apps in WebView</span>
          </a></li>
      <li><a href="../../../guide/webapps/debugging.html">
            <span class="en">Debugging Web Apps</span>
          </a></li>
      <li><a href="../../../guide/webapps/best-practices.html">
            <span class="en">Best Practices for Web Apps</span>
          </a></li>
    </ul>
  </li>

  <li>
    <h2><span class="en">Appendix</span>
               <span class="de" style="display:none">Anhang</span>
               <span class="es" style="display:none">Apéndice</span>
               <span class="fr" style="display:none">Annexes</span>
               <span class="it" style="display:none">Appendice</span>
               <span class="ja" style="display:none">付録</span>
               <span class="zh-CN" style="display:none">附录</span>
               <span class="zh-TW" style="display:none">附錄</span>
    </h2>
    <ul>
      <li><a href="../../../guide/appendix/api-levels.html">
            <span class="en">Android API Levels</span>
          </a></li>
      <li><a href="../../../guide/appendix/install-location.html">
            <span class="en">App Install Location</span>
          </a></li>
      <li><a href="../../../guide/appendix/media-formats.html">
            <span class="en">Supported Media Formats <span class="new">updated</span></span>
          </a></li>
      <li><a href="../../../guide/appendix/g-app-intents.html">
            <span class="en">Intents List: Google Apps</span>
          </a></li>
      <li><a href="../../../guide/developing/tools/aidl.html">AIDL</a></li>
      <li><a href="../../../guide/appendix/glossary.html">
            <span class="en">Glossary</span>
          </a></li>
    </ul>
  </li>

</ul>

<script type="text/javascript">
<!--
    buildToggleLists();
    changeNavLang(getLangPref());
//-->
</script>

      </div>
    </div> <!-- end side-nav -->
    <script>
      addLoadEvent(function() {
        scrollIntoView("devdoc-nav");
        });
    </script>




<div class="g-unit" id="doc-content"><a name="top"></a>

<div id="jd-header" class="guide-header">
  <span class="crumb">
    
      <a href="index.html">Multimedia and Camera</a> >
    
  </span>
<h1>Camera</h1>
</div>

  <div id="jd-content">

    <div class="jd-descr">
    <div id="qv-wrapper">
  <div id="qv">
  <h2>In this document</h2>
  <ol>
    <li><a href="#considerations">Considerations</a></li>
    <li><a href="#basics">The Basics</a>
    <li><a href="#manifest">Manifest Declarations</a></li>
    <li><a href="#intents">Using Existing Camera Apps</a>
      <ol>
        <li><a href="#intent-image">Image capture intent</a></li>
        <li><a href="#intent-video">Video capture intent</a></li>
        <li><a href="#intent-receive">Receiving camera intent result</a></li>
      </ol>
    <li><a href="#custom-camera">Building a Camera App</a>
      <ol>
        <li><a href="#detect-camera">Detecting camera hardware</a></li>
        <li><a href="#access-camera">Accessing cameras</a></li>
        <li><a href="#check-camera-features">Checking camera features</a></li>
        <li><a href="#camera-preview">Creating a preview class</a></li>
        <li><a href="#preview-layout">Placing preview in a layout</a></li>
        <li><a href="#capture-picture">Capturing pictures</a></li>
        <li><a href="#capture-video">Capturing videos</a></li>
        <li><a href="#release-camera">Releasing the camera</a></li>
      </ol>
    </li>
    <li><a href="#saving-media">Saving Media Files</a></li>
  </ol>
  <h2>Key Classes</h2>
  <ol>
    <li><code><a href="../../../reference/android/hardware/Camera.html">Camera</a></code></li>
    <li><code><a href="../../../reference/android/view/SurfaceView.html">SurfaceView</a></code></li>
    <li><code><a href="../../../reference/android/media/MediaRecorder.html">MediaRecorder</a></code></li>
    <li><code><a href="../../../reference/android/content/Intent.html">Intent</a></code></li>
  </ol>
  <h2>See also</h2>
  <ol>
    <li><a href="../../../reference/android/hardware/Camera.html">Camera</a></li>
    <li><a href="../../../reference/android/media/MediaRecorder.html">MediaRecorder</a></li>
    <li><a href="../../../guide/topics/data/data-storage.html">Data Storage</a></li>
  </ol>
  </div>
</div>


<p>The Android framework includes support for various cameras and camera features available on
devices, allowing you to capture pictures and videos in your applications. This document discusses a
quick, simple approach to image and video capture and outlines an advanced approach for creating
custom camera experiences for your users.</p>

<h2 id="considerations">Considerations</h2>
<p>Before enabling your application to use cameras on Android devices, you should consider a few
questions about how your app intends to use this hardware feature.</p>

<ul>
  <li><strong>Camera Requirement</strong> - Is the use of a camera so important to your
application that you do not want your application installed on a device that does not have a
camera? If so, you should declare the <a href="#manifest">camera requirement in your
manifest</a>.</li>

  <li><strong>Quick Picture or Customized Camera</strong> - How will your application use the
camera? Are you just interested in snapping a quick picture or video clip, or will your application
provide a new way to use cameras? For a getting a quick snap or clip, consider 
<a href="#intents">Using Existing Camera Apps</a>. For developing a customized camera feature, check
out the <a href="#custom-camera">Building a Camera App</a> section.</li>

  <li><strong>Storage</strong> - Are the images or videos your application generates intended to be
only visible to your application or shared so that other applications such as Gallery or other
media and social apps can use them? Do you want the pictures and videos to be available even if your
application is uninstalled? Check out the <a href="#saving-media">Saving Media Files</a> section to
see how to implement these options.</li>
</ul>



<h2 id="basics">The Basics</h2>
<p>The Android framework supports capturing images and video through the
<code><a href="../../../reference/android/hardware/Camera.html">Camera</a></code> API or camera <code><a href="../../../reference/android/content/Intent.html">Intent</a></code>. Here are the relevant
classes:</p>

<dl>
  <dt><code><a href="../../../reference/android/hardware/Camera.html">Camera</a></code></dt>
  <dd>This class is the primary API for controlling device cameras. This class is used to take
pictures or videos when you are building a camera application.</a>.</dd>

  <dt><code><a href="../../../reference/android/view/SurfaceView.html">SurfaceView</a></code></dt>
  <dd>This class is used to present a live camera preview to the user.</dd>

  <dt><code><a href="../../../reference/android/media/MediaRecorder.html">MediaRecorder</a></code></dt>
  <dd>This class is used to record video from the camera.</dd>

  <dt><code><a href="../../../reference/android/content/Intent.html">Intent</a></code></dt>
  <dd>An intent action type of <code><a href="../../../reference/android/provider/MediaStore.html#ACTION_IMAGE_CAPTURE">MediaStore.ACTION_IMAGE_CAPTURE</a></code> or <code><a href="../../../reference/android/provider/MediaStore.html#ACTION_VIDEO_CAPTURE">MediaStore.ACTION_VIDEO_CAPTURE</a></code> can be used to capture images or videos without directly
using the <code><a href="../../../reference/android/hardware/Camera.html">Camera</a></code> object.</dd>
</dl>


<h2 id="manifest">Manifest Declarations</h2>
<p>Before starting development on your application with the Camera API, you should make sure
your manifest has the appropriate declarations to allow use of camera hardware and other
related features.</p>

<ul>
  <li><strong>Camera Permission</strong> - Your application must request permission to use a device
camera.
<pre>
&lt;uses-permission android:name=&quot;android.permission.CAMERA&quot; /&gt;
</pre>
  <p class="note"><strong>Note:</strong> If you are using the camera <a href="#intents">via an
intent</a>, your application does not need to request this permission.</p>
  </li>
  <li><strong>Camera Features</strong> - Your application must also declare use of camera features,
for example:
<pre>
&lt;uses-feature android:name=&quot;android.hardware.camera&quot; /&gt;
</pre>
  <p>For a list of camera features, see the manifest <a
href="../../../guide/topics/manifest/uses-feature-element.html#features-reference">Features
Reference</a>.</p>
  <p>Adding camera features to your manifest causes Android Market to prevent your application from
being installed to devices that do not include a camera or do not support the camera features you
specify. For more information about using feature-based filtering with Android Market, see <a
href="../../../guide/topics/manifest/uses-feature-element.html#market-feature-filtering">Android
Market and Feature-Based Filtering</a>.</p>
  <p>If your application <em>can use</em> a camera or camera feature for proper operation, but does
not <em>require</em> it, you should specify this in the manifest by including the <code>android:required</code> attribute, and setting it to <code>false</code>:</p>
<pre>
&lt;uses-feature android:name="android.hardware.camera" android:required="false" /&gt;
</pre>

  </li>
  <li><strong>Storage Permission</strong> - If your application saves images or videos to the
device's external storage (SD Card), you must also specify this in the manifest.
<pre>
&lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; /&gt;
</pre>
  </li>
  <li><strong>Audio Recording Permission</strong> - For recording audio with video capture, your
application must request the audio capture permission.
<pre>
&lt;uses-permission android:name="android.permission.RECORD_AUDIO" /&gt;
</pre>
  </li>
</ul>


<h2 id="intents">Using Existing Camera Apps</h2>
<p>A quick way to enable taking pictures or videos in your application without a lot of extra code
is to use an <code><a href="../../../reference/android/content/Intent.html">Intent</a></code> to invoke an existing Android camera application. A
camera intent makes a request to capture a picture or video clip through an existing camera app and
then returns control back to your application. This section shows you how to capture an image or
video using this technique.</p>

<p>The procedure for invoking a camera intent follows these general steps:</p>

<ol>
  <li><strong>Compose a Camera Intent</strong> - Create an <code><a href="../../../reference/android/content/Intent.html">Intent</a></code> that
requests an image or video, using one of these intent types:
    <ul>
      <li><code><a href="../../../reference/android/provider/MediaStore.html#ACTION_IMAGE_CAPTURE">MediaStore.ACTION_IMAGE_CAPTURE</a></code> -
Intent action type for requesting an image from an existing camera application.</li>
      <li><code><a href="../../../reference/android/provider/MediaStore.html#ACTION_VIDEO_CAPTURE">MediaStore.ACTION_VIDEO_CAPTURE</a></code> -
Intent action type for requesting a video from an existing camera application. </li>
    </ul>
  </li>
  <li><strong>Start the Camera Intent</strong> - Use the <code><a href="../../../reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)">startActivityForResult()</a></code>
method to execute the camera intent. After you start the intent, the Camera application user
interface appears on the device screen and the user can take a picture or video.</li>
  <li><strong>Receive the Intent Result</strong> - Set up an <code><a href="../../../reference/android/app/Activity.html#onActivityResult(int, int, android.content.Intent)">onActivityResult()</a></code> method
in your application to receive the callback and data from the camera intent. When the user
finishes taking a picture or video (or cancels the operation), the system calls this method.</li>
</ol>


<h3 id="intent-image">Image capture intent</h3>
<p>Capturing images using a camera intent is quick way to enable your application to take pictures
with minimal coding. An image capture intent can include the following extra information:</p>

<ul>
  <li><code><a href="../../../reference/android/provider/MediaStore.html#EXTRA_OUTPUT">MediaStore.EXTRA_OUTPUT</a></code> - This setting
requires a <code><a href="../../../reference/android/net/Uri.html">Uri</a></code> object specifying a path and file name where you'd like to
save the picture. This setting is optional but strongly recommended. If you do not specify this
value, the camera application saves the requested picture in the default location with a default
name, specified in the returned intent's <code><a href="../../../reference/android/content/Intent.html#getData()">Intent.getData()</a></code>
field.</li>
</ul>

<p>The following example demonstrates how to construct a image capture intent and execute it.
The <code>getOutputMediaFileUri()</code> method in this example refers to the sample code shown in <a
href= "#saving-media">Saving Media Files</a>.</p>

<pre>
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Uri fileUri;

&#64;Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
</pre>

<p>When the <code><a href="../../../reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)">startActivityForResult()</a></code> method is executed, users see a camera application interface.
After the user finishes taking a picture (or cancels the operation), the user interface returns to
your application, and you must intercept the <code><a href="../../../reference/android/app/Activity.html#onActivityResult(int, int, android.content.Intent)">onActivityResult()</a></code>
method to receive the result of the intent and continue your application execution. For information
on how to receive the completed intent, see <a href="#intent-receive">Receiving Camera Intent
Result</a>.</p>


<h3 id="intent-video">Video capture intent</h3>
<p>Capturing video using a camera intent is a quick way to enable your application to take videos
with minimal coding. A video capture intent can include the following extra information:</p>

<ul>
  <li><code><a href="../../../reference/android/provider/MediaStore.html#EXTRA_OUTPUT">MediaStore.EXTRA_OUTPUT</a></code> - This setting
requires a <code><a href="../../../reference/android/net/Uri.html">Uri</a></code> specifying a path and file name where you'd like to save the
video. This setting is optional but strongly recommended. If you do not specify this value, the
Camera application saves the requested video in the default location with a default name, specified
in the returned intent's <code><a href="../../../reference/android/content/Intent.html#getData()">Intent.getData()</a></code> field.</li>
  <li><code><a href="../../../reference/android/provider/MediaStore.html#EXTRA_VIDEO_QUALITY">MediaStore.EXTRA_VIDEO_QUALITY</a></code> -
This value can be 0 for lowest quality and smallest file size or 1 for highest quality and
larger file size.</li>
  <li><code><a href="../../../reference/android/provider/MediaStore.html#EXTRA_DURATION_LIMIT">MediaStore.EXTRA_DURATION_LIMIT</a></code> -
Set this value to limit the length, in seconds, of the video being captured.</li>
  <li><code><a href="../../../reference/android/provider/MediaStore.html#EXTRA_SIZE_LIMIT">MediaStore.EXTRA_SIZE_LIMIT</a></code> -
Set this value to limit the file size, in bytes, of the video being captured.
</li>
</ul>

<p>The following example demonstrates how to construct a video capture intent and execute it.
The <code>getOutputMediaFileUri()</code> method in this example refers to the sample code shown in <a
href= "#saving-media">Saving Media Files</a>.</p>

<pre>
private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
private Uri fileUri;

&#64;Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //create new Intent
    Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);  // create a file to save the video
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);  // set the image file name

    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high

    // start the Video Capture Intent
    startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
}
</pre>

<p>When the <code><a href="../../../reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)">startActivityForResult()</a></code> method is executed, users see a modified camera application interface.
After the user finishes taking a video (or cancels the operation), the user interface
returns to your application, and you must intercept the <code><a href="../../../reference/android/app/Activity.html#onActivityResult(int, int, android.content.Intent)">onActivityResult()</a></code>
method to receive the result of the intent and continue your application execution. For information
on how to receive the completed intent, see the next section.</p>

<h3 id="intent-receive">Receiving camera intent result</h3>
<p>Once you have constructed and executed an image or video camera intent, your application must be
configured to receive the result of the intent. This section shows you how to intercept the callback
from a camera intent so your application can do further processing of the captured image or
video.</p>

<p>In order to receive the result of an intent, you must override the <code><a href="../../../reference/android/app/Activity.html#onActivityResult(int, int, android.content.Intent)">onActivityResult()</a></code> in the
activity that started the intent. The following example demonstrates how to override <code><a href="../../../reference/android/app/Activity.html#onActivityResult(int, int, android.content.Intent)">onActivityResult()</a></code> to
capture the result of the <a href="#intent-image">image camera intent</a> or <a
href="#intent-video">video camera intent</a> examples shown in the previous sections.</p>

<pre>
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;

&#64;Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Image captured and saved to fileUri specified in the Intent
            Toast.makeText(this, "Image saved to:\n" +
                     data.getData(), Toast.LENGTH_LONG).show();
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
        }
    }

    if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Video captured and saved to fileUri specified in the Intent
            Toast.makeText(this, "Video saved to:\n" +
                     data.getData(), Toast.LENGTH_LONG).show();
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the video capture
        } else {
            // Video capture failed, advise user
        }
    }
}
</pre>

<p>Once your activity receives a successful result, the captured image or video is available in the
specified location for your application to access.</p>



<h2 id="custom-camera">Building a Camera App</h2>
<p>Some developers may require a camera user interface that is customized to the look of their
application or provides special features. Creating a customized camera activity requires more
code than <a href="#intents">using an intent</a>, but it can provide a more compelling experience
for your users.</p>

<p>The general steps for creating a custom camera interface for your application are as follows:</p>

<ul>
   <li><strong>Detect and Access Camera</strong> - Create code to check for the existence of
cameras and request access.</li>
   <li><strong>Create a Preview Class</strong> - Create a camera preview class that extends <code><a href="../../../reference/android/view/SurfaceView.html">SurfaceView</a></code> and implements the <code><a href="../../../reference/android/view/SurfaceHolder.html">SurfaceHolder</a></code> interface. This
class previews the live images from the camera.</li>
   <li><strong>Build a Preview Layout</strong> - Once you have the camera preview class, create a
view layout that incorporates the preview and the user interface controls you want.</li>
   <li><strong>Setup Listeners for Capture</strong> - Connect listeners for your interface
controls to start image or video capture in response to user actions, such as pressing a
button.</li>
   <li><strong>Capture and Save Files</strong> - Setup the code for capturing pictures or
videos and saving the output.</li>
   <li><strong>Release the Camera</strong> - After using the camera, your application must
properly release it for use by other applications.</li>
</ul>

<p>Camera hardware is a shared resource that must be carefully managed so your application does
not collide with other applications that may also want to use it. The following sections discusses
how to detect camera hardware, how to request access to a camera and how to release it when your
application is done using it.</p>

<p class="caution"><strong>Caution:</strong> Remember to release the <code><a href="../../../reference/android/hardware/Camera.html">Camera</a></code>
object by calling the <code><a href="../../../reference/android/hardware/Camera.html#release()">Camera.release()</a></code> when your
application is done using it! If your application does not properly release the camera, all
subsequent attempts to access the camera, including those by your own application, will fail and may
cause your or other applications to be shut down.</p>


<h3 id="detect-camera">Detecting camera hardware</h3>
<p>If your application does not specifically require a camera using a manifest declaration, you
should check to see if a camera is available at runtime. To perform this check, use the <code><a href="../../../reference/android/content/pm/PackageManager.html#hasSystemFeature(java.lang.String)">PackageManager.hasSystemFeature()</a></code> method, as shown in the example code below:</p>

<pre>
/** Check if this device has a camera */
private boolean checkCameraHardware(Context context) {
    if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
        // this device has a camera
        return true;
    } else {
        // no camera on this device
        return false;
    }
}
</pre>

<p>Android devices can have multiple cameras, for example a back-facing camera for photography and a
front-facing camera for video calls. Android 2.3 (API Level 9) and later allows you to check the
number of cameras available on a device using the <code><a href="../../../reference/android/hardware/Camera.html#getNumberOfCameras()">Camera.getNumberOfCameras()</a></code> method.</p>

<h3 id="access-camera">Accessing cameras</h3>
<p>If you have determined that the device on which your application is running has a camera, you
must request to access it by getting an instance of <code><a href="../../../reference/android/hardware/Camera.html">Camera</a></code> (unless you
are using an <a href="#intents">intent to access the camera</a>). </p>

<p>To access the primary camera, use the <code><a href="../../../reference/android/hardware/Camera.html#open()">Camera.open()</a></code> method
and be sure to catch any exceptions, as shown in the code below:</p>

<pre>
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}
</pre>

<p class="caution"><strong>Caution:</strong> Always check for exceptions when using <code><a href="../../../reference/android/hardware/Camera.html#open()">Camera.open()</a></code>. Failing to check for exceptions if the camera is in
use or does not exist will cause your application to be shut down by the system.</p>

<p>On devices running Android 2.3 (API Level 9) or higher, you can access specific cameras using
<code><a href="../../../reference/android/hardware/Camera.html#open(int)">Camera.open(int)</a></code>. The example code above will access
the first, back-facing camera on a device with more than one camera.</p>

<h3 id="check-camera-features">Checking camera features</h3>
<p>Once you obtain access to a camera, you can get further information about its capabilties using
the <code><a href="../../../reference/android/hardware/Camera.html#getParameters()">Camera.getParameters()</a></code> method and checking the
returned <code><a href="../../../reference/android/hardware/Camera.Parameters.html">Camera.Parameters</a></code> object for supported capabilities. When using
API Level 9 or higher, use the <code><a href="../../../reference/android/hardware/Camera.html#getCameraInfo(int, android.hardware.Camera.CameraInfo)">Camera.getCameraInfo()</a></code> to determine if a camera is on the front
or back of the device, and the orientation of the image.</p>



<h3 id="camera-preview">Creating a preview class</h3>
<p>For users to effectively take pictures or video, they must be able to see what the device camera
sees. A camera preview class is a <code><a href="../../../reference/android/view/SurfaceView.html">SurfaceView</a></code> that can display the live image
data coming from a camera, so users can frame and capture a picture or video.</p>

<p>The following example code demonstrates how to create a basic camera preview class that can be
included in a <code><a href="../../../reference/android/view/View.html">View</a></code> layout. This class implements <code><a href="../../../reference/android/view/SurfaceHolder.Callback.html">SurfaceHolder.Callback</a></code> in order to capture the callback events
for creating and destroying the view, which are needed for assigning the camera preview input.</p>

<pre>
/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;

    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
            Log.d(TAG, "Error setting camera preview: " + e.getMessage());
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // empty. Take care of releasing the Camera preview in your activity.
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null){
          // preview surface does not exist
          return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
          // ignore: tried to stop a non-existent preview
        }

        // make any resize, rotate or reformatting changes here

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e){
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }
}
</pre>


<h3 id="preview-layout">Placing preview in a layout</h3>
<p>A camera preview class, such as the example shown in the previous section, must be placed in the
layout of an activity along with other user interface controls for taking a picture or video. This
section shows you how to build a basic layout and activity for the preview.</p>

<p>The following layout code provides a very basic view that can be used to display a camera
preview. In this example, the <code><a href="../../../reference/android/widget/FrameLayout.html">FrameLayout</a></code> element is meant to be the
container for the camera preview class. This layout type is used so that additional picture
information or controls can be overlayed on the live camera preview images.</p>

<pre>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:orientation=&quot;horizontal&quot;
    android:layout_width=&quot;fill_parent&quot;
    android:layout_height=&quot;fill_parent&quot;
    &gt;
  &lt;FrameLayout
    android:id=&quot;@+id/camera_preview&quot;
    android:layout_width=&quot;fill_parent&quot;
    android:layout_height=&quot;fill_parent&quot;
    android:layout_weight=&quot;1&quot;
    /&gt;

  &lt;Button
    android:id=&quot;@+id/button_capture&quot;
    android:text=&quot;Capture&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_gravity=&quot;center&quot;
    /&gt;
&lt;/LinearLayout&gt;
</pre>

<p>On most devices, the default orientation of the camera preview is landscape. This example layout
specifies a horizontal (landscape) layout and the code below fixes the orientation of the
application to landscape. For simplicity in rendering a camera preview, you should change your
application's preview activity orientation to landscape by adding the following to your
manifest.</p>

<pre>
&lt;activity android:name=&quot;.CameraActivity&quot;
          android:label=&quot;@string/app_name&quot;

          android:screenOrientation=&quot;landscape&quot;&gt;
          &lt;!-- configure this activity to use landscape orientation --&gt;

          &lt;intent-filter&gt;
        &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
        &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
    &lt;/intent-filter&gt;
&lt;/activity&gt;
</pre>

<p class="note"><strong>Note:</strong> A camera preview does not have to be in landscape mode.
Starting in Android 2.2 (API Level 8), you can use the <code><a href="../../../reference/android/hardware/Camera.html#setDisplayOrientation(int)">setDisplayOrientation()</a></code> method to set the
rotation of the preview image. In order to change preview orientation as the user re-orients the
phone, within the <code><a href="../../../reference/android/view/SurfaceHolder.Callback.html#surfaceChanged(android.view.SurfaceHolder, int, int, int)">surfaceChanged()</a></code> method of your preview class, first stop the preview with <code><a href="../../../reference/android/hardware/Camera.html#stopPreview()">Camera.stopPreview()</a></code> change the orientation and then
start the preview again with <code><a href="../../../reference/android/hardware/Camera.html#startPreview()">Camera.startPreview()</a></code>.</p>

<p>In the activity for your camera view, add your preview class to the <code><a href="../../../reference/android/widget/FrameLayout.html">FrameLayout</a></code> element shown in the example above. Your camera activity must also
ensure that it releases the camera when it is paused or shut down. The following example shows how
to modify a camera activity to attach the preview class shown in <a href="#camera-preview">Creating
a preview class</a>.</p>

<pre>
public class CameraActivity extends Activity {

    private Camera mCamera;
    private CameraPreview mPreview;

    &#64;Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Create an instance of Camera
        mCamera = getCameraInstance();

        // Create our Preview view and set it as the content of our activity.
        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(id.camera_preview);
        preview.addView(mPreview);
    }
}
</pre>

<p class="note"><strong>Note:</strong> The <code>getCameraInstance()</code> method in the example above
refers to the example method shown in <a href="#access-camera">Accessing cameras</a>.</p>


<h3 id="capture-picture">Capturing pictures</h3>
<p>Once you have built a preview class and a view layout in which to display it, you are ready to
start capturing images with your application. In your application code, you must set up listeners
for your user interface controls to respond to a user action by taking a picture.</p>

<p>In order to retrieve a picture, use the <code><a href="../../../reference/android/hardware/Camera.html#takePicture(android.hardware.Camera.ShutterCallback, android.hardware.Camera.PictureCallback, android.hardware.Camera.PictureCallback)">Camera.takePicture()</a></code> method. This method takes three parameters which receive data from the camera.
In order to receive data in a JPEG format, you must implement an <code><a href="../../../reference/android/hardware/Camera.PictureCallback.html">Camera.PictureCallback</a></code> interface to receive the image data and
write it to a file. The following code shows a basic implementation of the <code><a href="../../../reference/android/hardware/Camera.PictureCallback.html">Camera.PictureCallback</a></code> interface to save an image received from the camera.</p>

<pre>
private PictureCallback mPicture = new PictureCallback() {

    &#64;Override
    public void onPictureTaken(byte[] data, Camera camera) {

        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        if (pictureFile == null){
            Log.d(TAG, "Error creating media file, check storage permissions: " +
                e.getMessage());
            return;
        }

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        } catch (FileNotFoundException e) {
            Log.d(TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d(TAG, "Error accessing file: " + e.getMessage());
        }
    }
};
</pre>

<p>Trigger capturing an image by calling the <code><a href="../../../reference/android/hardware/Camera.html#takePicture(android.hardware.Camera.ShutterCallback, android.hardware.Camera.PictureCallback, android.hardware.Camera.PictureCallback)">Camera.takePicture()</a></code> method. The following example code shows how to call this method from a
button <code><a href="../../../reference/android/view/View.OnClickListener.html">View.OnClickListener</a></code>.</p>

<pre>
// Add a listener to the Capture button
Button captureButton = (Button) findViewById(id.button_capture);
    captureButton.setOnClickListener(
        new View.OnClickListener() {
        &#64;Override
        public void onClick(View v) {
            // get an image from the camera
            mCamera.takePicture(null, null, mPicture);
        }
    }
);
</pre>

<p class="note"><strong>Note:</strong> The <code>mPicture</code> member in the following example refers
to the example code above.</p>

<p class="caution"><strong>Caution:</strong> Remember to release the <code><a href="../../../reference/android/hardware/Camera.html">Camera</a></code>
object by calling the <code><a href="../../../reference/android/hardware/Camera.html#release()">Camera.release()</a></code> when your
application is done using it! For information about how to release the camera, see <a
href="#release-camera">Releasing the camera</a>.</p>


<h3 id="capture-video">Capturing videos</h3>

<p>Video capture using the Android framework requires careful management of the <code><a href="../../../reference/android/hardware/Camera.html">Camera</a></code> object and coordination with the <code><a href="../../../reference/android/media/MediaRecorder.html">MediaRecorder</a></code>
class. When recording video with <code><a href="../../../reference/android/hardware/Camera.html">Camera</a></code>, you must manage the <code><a href="../../../reference/android/hardware/Camera.html#lock()">Camera.lock()</a></code> and <code><a href="../../../reference/android/hardware/Camera.html#unlock()">Camera.unlock()</a></code> calls to allow <code><a href="../../../reference/android/media/MediaRecorder.html">MediaRecorder</a></code> access to the camera hardware,
in addition to the <code><a href="../../../reference/android/hardware/Camera.html#open()">Camera.open()</a></code> and <code><a href="../../../reference/android/hardware/Camera.html#release()">Camera.release()</a></code> calls.</p>

<p class="note"><strong>Note:</strong> Starting with Android 4.0 (API level 14), the <code><a href="../../../reference/android/hardware/Camera.html#lock()">Camera.lock()</a></code> and <code><a href="../../../reference/android/hardware/Camera.html#unlock()">Camera.unlock()</a></code> calls are managed for you automatically.</p>

<p>Unlike taking pictures with a device camera, capturing video requires a very particular call
order. You must follow a specific order of execution to successfully prepare for and capture video
with your application, as detailed below.</p>

<ol>
  <li><strong>Open Camera</strong> - Use the <code><a href="../../../reference/android/hardware/Camera.html#open()">Camera.open()</a></code>
to get an instance of the camera object.</li>
  <li><strong>Connect Preview</strong> - Prepare a live camera image preview by connecting a <code><a href="../../../reference/android/view/SurfaceView.html">SurfaceView</a></code> to the camera using <code><a href="../../../reference/android/hardware/Camera.html#setPreviewDisplay(android.view.SurfaceHolder)">Camera.setPreviewDisplay()</a></code>.
  </li>
  <li><strong>Start Preview</strong> - Call <code><a href="../../../reference/android/hardware/Camera.html#startPreview()">Camera.startPreview()</a></code> to begin displaying the live camera images.</li>
  <li><strong>Start Recording Video</strong> - The following steps must be completed <em>in
order</em> to successfully record video:
    <ol style="list-style-type: lower-alpha;">
      <li><strong>Unlock the Camera</strong> - Unlock the camera for use by <code><a href="../../../reference/android/media/MediaRecorder.html">MediaRecorder</a></code> by calling <code><a href="../../../reference/android/hardware/Camera.html#unlock()">Camera.unlock()</a></code>.</li>
      <li><strong>Configure MediaRecorder</strong> - Call in the following <code><a href="../../../reference/android/media/MediaRecorder.html">MediaRecorder</a></code> methods <em>in this order</em>. For more information, see the <code><a href="../../../reference/android/media/MediaRecorder.html">MediaRecorder</a></code> reference documentation.
        <ol>
          <li><code><a href="../../../reference/android/media/MediaRecorder.html#setCamera(android.hardware.Camera)">setCamera()</a></code> - Set the camera to be used for video capture, use your application's current instance
of <code><a href="../../../reference/android/hardware/Camera.html">Camera</a></code>.</li>
          <li><code><a href="../../../reference/android/media/MediaRecorder.html#setAudioSource(int)">setAudioSource()</a></code> - Set the
audio source, use <code><a href="../../../reference/android/media/MediaRecorder.AudioSource.html#CAMCORDER">MediaRecorder.AudioSource.CAMCORDER</a></code>. </li>
          <li><code><a href="../../../reference/android/media/MediaRecorder.html#setVideoSource(int)">setVideoSource()</a></code> - Set
the video source, use <code><a href="../../../reference/android/media/MediaRecorder.VideoSource.html#CAMERA">MediaRecorder.VideoSource.CAMERA</a></code>.</li>
          <li>Set the video output format and encoding. For Android 2.2 (API Level 8) and
higher, use the <code><a href="../../../reference/android/media/MediaRecorder.html#setProfile(android.media.CamcorderProfile)">MediaRecorder.setProfile</a></code> method, and get a profile instance using <code><a href="../../../reference/android/media/CamcorderProfile.html#get(int)">CamcorderProfile.get()</a></code>. For versions of Android prior to
2.2, you must set the video output format and encoding parameters:
          <ol style="list-style-type: lower-roman;">
            <li><code><a href="../../../reference/android/media/MediaRecorder.html#setOutputFormat(int)">setOutputFormat()</a></code> - Set
the output format, specify the default setting or <code><a href="../../../reference/android/media/MediaRecorder.OutputFormat.html#MPEG_4">MediaRecorder.OutputFormat.MPEG_4</a></code>.</li>
            <li><code><a href="../../../reference/android/media/MediaRecorder.html#setAudioEncoder(int)">setAudioEncoder()</a></code> - Set
the sound encoding type, specify the default setting or <code><a href="../../../reference/android/media/MediaRecorder.AudioEncoder.html#AMR_NB">MediaRecorder.AudioEncoder.AMR_NB</a></code>.</li>
            <li><code><a href="../../../reference/android/media/MediaRecorder.html#setVideoEncoder(int)">setVideoEncoder()</a></code> - Set
the video encoding type, specify the default setting or <code><a href="../../../reference/android/media/MediaRecorder.VideoEncoder.html#MPEG_4_SP">MediaRecorder.VideoEncoder.MPEG_4_SP</a></code>.</li>
          </ol>
          </li>
          <li><code><a href="../../../reference/android/media/MediaRecorder.html#setOutputFile(java.lang.String)">setOutputFile()</a></code> -
Set the output file, use <code>getOutputMediaFile(MEDIA_TYPE_VIDEO).toString()</code> from the example
method in the <a href="#saving-media">Saving Media Files</a> section.</li>
          <li><code><a href="../../../reference/android/media/MediaRecorder.html#setPreviewDisplay(android.view.Surface)">setPreviewDisplay()</a></code> - Specify the <code><a href="../../../reference/android/view/SurfaceView.html">SurfaceView</a></code> preview layout element for
your application. Use the same object you specified for <strong>Connect Preview</strong>.</li>
        </ol>
        <p class="caution"><strong>Caution:</strong> You must call these <code><a href="../../../reference/android/media/MediaRecorder.html">MediaRecorder</a></code> configuration methods <em>in this order</em>, otherwise your
application will encounter errors and the recording will fail.</p>
      </li>
      <li><strong>Prepare MediaRecorder</strong> - Prepare the <code><a href="../../../reference/android/media/MediaRecorder.html">MediaRecorder</a></code>
with provided configuration settings by calling <code><a href="../../../reference/android/media/MediaRecorder.html#prepare()">MediaRecorder.prepare()</a></code>.</li>
      <li><strong>Start MediaRecorder</strong> - Start recording video by calling <code><a href="../../../reference/android/media/MediaRecorder.html#start()">MediaRecorder.start()</a></code>.</li>
    </ol>
  </li>
  <li><strong>Stop Recording Video</strong> - Call the following methods <em>in order</em>, to
successfully complete a video recording:
    <ol style="list-style-type: lower-alpha;">
      <li><strong>Stop MediaRecorder</strong> - Stop recording video by calling <code><a href="../../../reference/android/media/MediaRecorder.html#stop()">MediaRecorder.stop()</a></code>.</li>
      <li><strong>Reset MediaRecorder</strong> - Optionally, remove the configuration settings from
the recorder by calling <code><a href="../../../reference/android/media/MediaRecorder.html#reset()">MediaRecorder.reset()</a></code>.</li>
      <li><strong>Release MediaRecorder</strong> - Release the <code><a href="../../../reference/android/media/MediaRecorder.html">MediaRecorder</a></code>
by calling <code><a href="../../../reference/android/media/MediaRecorder.html#release()">MediaRecorder.release()</a></code>.</li>
      <li><strong>Lock the Camera</strong> - Lock the camera so that future <code><a href="../../../reference/android/media/MediaRecorder.html">MediaRecorder</a></code> sessions can use it by calling <code><a href="../../../reference/android/hardware/Camera.html#lock()">Camera.lock()</a></code>. Starting with Android 4.0 (API level 14), this call is not required unless the
<code><a href="../../../reference/android/media/MediaRecorder.html#prepare()">MediaRecorder.prepare()</a></code> call fails.</li>
    </ol>
  </li>
  <li><strong>Stop the Preview</strong> - When your activity has finished using the camera, stop the
preview using <code><a href="../../../reference/android/hardware/Camera.html#stopPreview()">Camera.stopPreview()</a></code>.</li>
  <li><strong>Release Camera</strong> - Release the camera so that other applications can use
it by calling <code><a href="../../../reference/android/hardware/Camera.html#release()">Camera.release()</a></code>.</li>
</ol>

<p class="note"><strong>Note:</strong> It is possible to use <code><a href="../../../reference/android/media/MediaRecorder.html">MediaRecorder</a></code>
without creating a camera preview first and skip the first few steps of this process. However,
since users typically prefer to see a preview before starting a recording, that process is not
discussed here.</p>

<h4 id="configuring-mediarecorder">Configuring MediaRecorder</h4>
<p>When using the <code><a href="../../../reference/android/media/MediaRecorder.html">MediaRecorder</a></code> class to record video, you must perform
configuration steps in a <em>specific order</em> and then call the <code><a href="../../../reference/android/media/MediaRecorder.html#prepare()">MediaRecorder.prepare()</a></code> method to check and implement the
configuration. The following example code demonstrates how to properly configure and prepare the
<code><a href="../../../reference/android/media/MediaRecorder.html">MediaRecorder</a></code> class for video recording.</p>

<pre>
private boolean prepareVideoRecorder(){

    mCamera = getCameraInstance();
    mMediaRecorder = new MediaRecorder();

    // Step 1: Unlock and set camera to MediaRecorder
    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);

    // Step 2: Set sources
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
    mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

    // Step 4: Set output file
    mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());

    // Step 5: Set the preview output
    mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());

    // Step 6: Prepare configured MediaRecorder
    try {
        mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
        Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    }
    return true;
}
</pre>

<p>Prior to Android 2.2 (API Level 8), you must set the output format and encoding formats
parameters directly, instead of using <code><a href="../../../reference/android/media/CamcorderProfile.html">CamcorderProfile</a></code>. This approach is
demonstrated in the following code:</p>

<pre>
    // Step 3: Set output format and encoding (for versions prior to API Level 8)
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
</pre>

<p>The following video recording parameters for <code><a href="../../../reference/android/media/MediaRecorder.html">MediaRecorder</a></code> are given
default settings, however, you may want to adjust these settings for your application:</p>

<ul>
  <li><code><a href="../../../reference/android/media/MediaRecorder.html#setVideoEncodingBitRate(int)">setVideoEncodingBitRate()</a></code></li>
  <li><code><a href="../../../reference/android/media/MediaRecorder.html#setVideoSize(int, int)">setVideoSize()</a></code></li>
  <li><code><a href="../../../reference/android/media/MediaRecorder.html#setVideoFrameRate(int)">setVideoFrameRate()</a></code></li>
  <li><code><a href="../../../reference/android/media/MediaRecorder.html#setAudioEncodingBitRate(int)">setAudioEncodingBitRate()</a></code></li>  <li><code><a href="../../../reference/android/media/MediaRecorder.html#setAudioChannels(int)">setAudioChannels()</a></code></li>
  <li><code><a href="../../../reference/android/media/MediaRecorder.html#setAudioSamplingRate(int)">setAudioSamplingRate()</a></code></li>
</ul>

<h4 id="start-stop-mediarecorder">Starting and Stopping MediaRecorder</h4>
<p>When starting and stopping video recording using the <code><a href="../../../reference/android/media/MediaRecorder.html">MediaRecorder</a></code> class,
you must follow a specific order, as listed below.</p>

<ol>
  <li>Unlock the camera with <code><a href="../../../reference/android/hardware/Camera.html#unlock()">Camera.unlock()</a></code></li>
  <li>Configure <code><a href="../../../reference/android/media/MediaRecorder.html">MediaRecorder</a></code> as shown in the code example above</li>
  <li>Start recording using <code><a href="../../../reference/android/media/MediaRecorder.html#start()">MediaRecorder.start()</a></code></li>
  <li>Record the video</li>
  <li>Stop recording using <code><a href="../../../reference/android/media/MediaRecorder.html#stop()">MediaRecorder.stop()</a></code></li>
  <li>Release the media recorder with <code><a href="../../../reference/android/media/MediaRecorder.html#release()">MediaRecorder.release()</a></code></li>
  <li>Lock the camera using <code><a href="../../../reference/android/hardware/Camera.html#lock()">Camera.lock()</a></code></li>
</ol>

<p>The following example code demonstrates how to wire up a button to properly start and stop
video recording using the camera and the <code><a href="../../../reference/android/media/MediaRecorder.html">MediaRecorder</a></code> class.</p>

<p class="note"><strong>Note:</strong> When completing a video recording, do not release the camera
or else your preview will be stopped.</p>

<pre>
private boolean isRecording = false;

// Add a listener to the Capture button
Button captureButton = (Button) findViewById(id.button_capture);
captureButton.setOnClickListener(
    new View.OnClickListener() {
        &#64;Override
        public void onClick(View v) {
            if (isRecording) {
                // stop recording and release camera
                mMediaRecorder.stop();  // stop the recording
                releaseMediaRecorder(); // release the MediaRecorder object
                mCamera.lock();         // take camera access back from MediaRecorder

                // inform the user that recording has stopped
                setCaptureButtonText("Capture");
                isRecording = false;
            } else {
                // initialize video camera
                if (prepareVideoRecorder()) {
                    // Camera is available and unlocked, MediaRecorder is prepared,
                    // now you can start recording
                    mMediaRecorder.start();

                    // inform the user that recording has started
                    setCaptureButtonText("Stop");
                    isRecording = true;
                } else {
                    // prepare didn't work, release the camera
                    releaseMediaRecorder();
                    // inform user
                }
            }
        }
    }
);
</pre>

<p class="note"><strong>Note:</strong> In the above example, the <code>prepareVideoRecorder()</code>
method refers to the example code shown in <a
href="#configuring-mediarecorder">Configuring MediaRecorder</a>. This method takes care of locking
the camera, configuring and preparing the <code><a href="../../../reference/android/media/MediaRecorder.html">MediaRecorder</a></code> instance.</p>


<h3 id="release-camera">Releasing the camera</h3>
<p>Cameras are a resource that is shared by applications on a device. Your application can make
use of the camera after getting an instance of <code><a href="../../../reference/android/hardware/Camera.html">Camera</a></code>, and you must be
particularly careful to release the camera object when your application stops using it, and as
soon as your application is paused (<code><a href="../../../reference/android/app/Activity.html#onPause()">Activity.onPause()</a></code>). If
your application does not properly release the camera, all subsequent attempts to access the camera,
including those by your own application, will fail and may cause your or other applications to be
shut down.</p>

<p>To release an instance of the <code><a href="../../../reference/android/hardware/Camera.html">Camera</a></code> object, use the <code><a href="../../../reference/android/hardware/Camera.html#release()">Camera.release()</a></code> method, as shown in the example code below.</p>

<pre>
public class CameraActivity extends Activity {
    private Camera mCamera;
    private SurfaceView mPreview;
    private MediaRecorder mMediaRecorder;

    ...
    
    &#64;Override
    protected void onPause() {
        super.onPause();
        releaseMediaRecorder();       // if you are using MediaRecorder, release it first
        releaseCamera();              // release the camera immediately on pause event
    }

    private void releaseMediaRecorder(){
        if (mMediaRecorder != null) {
            mMediaRecorder.reset();   // clear recorder configuration
            mMediaRecorder.release(); // release the recorder object
            mMediaRecorder = null;
            mCamera.lock();           // lock camera for later use
        }
    }

    private void releaseCamera(){
        if (mCamera != null){
            mCamera.release();        // release the camera for other applications
            mCamera = null;
        }
    }
}
</pre>

<p class="caution"><strong>Caution:</strong> If your application does not properly release the
camera, all subsequent attempts to access the camera, including those by your own application, will
fail and may cause your or other applications to be shut down.</p>


<h2 id="saving-media">Saving Media Files</h2>
<p>Media files created by users such as pictures and videos should be saved to a device's external
storage directory (SD Card) to conserve system space and to allow users to access these files
without their device. There are many possible directory locations to save media files on a device,
however there are only two standard locations you should consider as a developer:</p>

<ul>
  <li><strong><code><a href="../../../reference/android/os/Environment.html#getExternalStoragePublicDirectory(java.lang.String)">Environment.getExternalStoragePublicDirectory</a></code>(<code><a href="../../../reference/android/os/Environment.html#DIRECTORY_PICTURES">Environment.DIRECTORY_PICTURES</a></code>)</strong> - This method returns the standard, shared and recommended
location for saving pictures and videos. This directory is shared (public), so other applications
can easily discover, read, change and delete files saved in this location. If your application is
uninstalled by the user, media files saved to this location will not be removed. To avoid
interfering with users existing pictures and videos, you should create a sub-directory for your
application's media files within this directory, as shown in the code sample below. This method is
available in Android 2.2 (API Level 8), for equivalent calls in earlier API versions, see <a
href="../../../guide/topics/data/data-storage.html#SavingSharedFiles">Saving Shared Files</a>.</li>
  <li><strong><code><a href="../../../reference/android/content/Context.html#getExternalFilesDir(java.lang.String)">Context.getExternalFilesDir</a></code>(<code><a href="../../../reference/android/os/Environment.html#DIRECTORY_PICTURES">Environment.DIRECTORY_PICTURES</a></code>)</strong> - This method returns a standard location for saving
pictures and videos which are associated with your application. If your application is uninstalled,
any files saved in this location are removed. Security is not enforced for files in this
location and other applications may read, change and delete them.</li>
</ul>

<p>The following example code demonstrates how to create a <code><a href="../../../reference/java/io/File.html">File</a></code> or <code><a href="../../../reference/android/net/Uri.html">Uri</a></code> location for a media file that can be used when invoking a device's camera with
an <code><a href="../../../reference/android/content/Intent.html">Intent</a></code> or as part of a <a href="#custom-camera">Building a Camera
App</a>.</p>

<pre>
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;

/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
      return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
private static Uri getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "MyCameraApp");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");
    } else if(type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "VID_"+ timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}
</pre>

<p class="note"><strong>Note:</strong> <code><a href="../../../reference/android/os/Environment.html#getExternalStoragePublicDirectory(java.lang.String)">Environment.getExternalStoragePublicDirectory()</a></code> is available in Android 2.2 (API Level 8) or
higher. If you are targeting devices with earlier versions of Android, use <code><a href="../../../reference/android/os/Environment.html#getExternalStorageDirectory()">Environment.getExternalStorageDirectory()</a></code>
instead. For more information, see <a
href="../../../guide/topics/data/data-storage.html#SavingSharedFiles">Saving Shared Files</a>.</p>

<p>For more information about saving files on an Android device, see <a
href="../../../guide/topics/data/data-storage.html">Data Storage</a>.</p>
    </div>

  <a href="#top" style="float:right">&uarr; Go to top</a>
  
    <p><a href="index.html">&larr; Back to Multimedia and Camera</a></p>
  
  </div>

<div id="footer">


  <div id="copyright">
    
  Except as noted, this content is licensed under <a
  href="http://www.apache.org/licenses/LICENSE-2.0">Apache 2.0</a>. 
  For details and restrictions, see the <a href="../../../license.html">
  Content License</a>.
  </div>
  <div id="build_info">
    
  Android 4.0&nbsp;r1 - 17 Oct 2011 18:17

  </div>

  <div id="footerlinks">
    
  <p>
    <a href="http://www.android.com/terms.html">Site Terms of Service</a> -
    <a href="http://www.android.com/privacy.html">Privacy Policy</a> -
    <a href="http://www.android.com/branding.html">Brand Guidelines</a>
  </p>
  </div>

</div> <!-- end footer -->

</div><!-- end doc-content -->

</div> <!-- end body-content --> 

<script type="text/javascript">
init(); /* initialize android-developer-docs.js */
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-5831155-1");
pageTracker._trackPageview();
</script>

</body>
</html>



